package com.xingchi.tornado.test.controller;

import com.xingchi.tornado.basic.Result;
import com.xingchi.tornado.log.aspect.anno.OperateLog;
import com.xingchi.tornado.test.model.Person;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * web切面控制器配置
 *
 * @author xiaoya
 * @date 2024/9/26 14:43
 */
@RestController("/weblog")
public class WebLogAspectTestController {

    private static final Logger log = LoggerFactory.getLogger(WebLogAspectTestController.class);

    @GetMapping("/testPath/{path}")
    public Result<String> testPath(@PathVariable("path") String path,
                           @RequestParam("id") String id,
                           @RequestParam("name") String name) {
        log.info("测试路径参数和query参数混合");
        return Result.ok(path + "|" + id + "|" + name);
    }

    @GetMapping("/testStrReturn")
    public String testStrReturn() {
        log.info("测试直接返回str");
        return "ok!";
    }

    @GetMapping("/testNullReturn")
    public void testNullReturn() {
        log.info("返回值为空");
    }

    @PostMapping("/testJsonPara")
    public Person testJsonPara(@RequestBody Person person) {
        log.info("测试json参数");
        person.setId(UUID.randomUUID().toString().replace("-", ""));
        return person;
    }

    @PostMapping("/testListParam")
    public void testJsonPara(@RequestBody List<Person> persons, HttpServletRequest request, HttpServletResponse response) {
        log.info("测试集合参数，同时携带其他参数");
    }

    @GetMapping("/test/slow")
    @OperateLog(module = "日志测试", operate = "测试慢接口")
    public Result<Object> testSlow() throws InterruptedException {
        log.info("测试慢接口");
        Thread.sleep(600);
        return Result.ok();
    }

    @GetMapping("/test/throw")
    @OperateLog(module = "日志测试", operate = "测试抛出异常")
    public Result<Object> testThrow(String isThrow) throws InterruptedException {
        if ("1".equals(isThrow)) {
            throw new RuntimeException("测试异常抛出");
        }
        return Result.ok();
    }

}
